package com.yazi.mxz.server.controller;

import java.util.List;

import javax.annotation.Resource;

import com.fasterxml.jackson.annotation.JsonView;
import com.yazi.mxz.common.entity.beans.Category;
import com.yazi.mxz.common.service.CategoryService;
import com.yazi.mxz.server.request.ListRequest;
import com.yazi.mxz.server.response.CategoryListResponse;
import com.yazi.mxz.server.response.view.CategoriesView;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller("categoryController")
@RequestMapping("/category")
public class CategoryController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(CategoryController.class);
	
	@Resource
	CategoryService categoryService;
	
	@RequestMapping("/list")
	@ResponseBody
	@JsonView(CategoriesView.class)
	public CategoryListResponse list(@RequestBody ListRequest<Category> req) {
		log.debug("客户端请求 -> 获取商品分类列表");
		CategoryListResponse res = new CategoryListResponse();
		try {
			List<Category> categoryList = categoryService.search(req.getQueryObject(), req.getPageInfo(), req.getSearchWord());
			res.setSuccessData(categoryList);
			res.setOkMessage("ok");
		} catch (Exception e) {
			log.error("客户端获取商品分类列表异常:", e);
			res.setErrorMessage(e.getMessage());
		}
		return res;
	}
}
